<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Senior Components - Vue Lesson 5</title>
    <script src="bower_components/vue/dist/vue.min.js"></script>
</head>
<body>

<!--局部注册-->
<div id="example">
    <div>A custom component!</div>
    <my-component></my-component>
</div>
<script type="text/javascript">
    var Child = {
        template: '<div>A custom component!</div>'
    }


    new Vue({
        el: '#example',
        components: {
            // <my-component> 将只在父模板可用
            'my-component': Child
        }
    });

</script>


<!--自定义事件-->

<div id="counter-event-example">
    <p>{{ total }}</p>
    <button-counter v-on:increment="incrementTotal"></button-counter>
    <button-counter v-on:increment="incrementTotal"></button-counter>
</div>
<script>
    Vue.component('button-counter', {
        template: '<button v-on:click="increment">{{ counter }}</button>',

        data: function () {
            return {
                counter: 0
            }
        },
        methods: {
            increment: function () {
                this.counter += 1
                this.$emit('increment') //报告自己的内部事件
            }
        },
    });

    new Vue({
        el: '#counter-event-example',
        data: {
            total: 0
        },
        methods: {
            incrementTotal: function () {
                this.total += 1
            }
        }
    })
</script>



</body>
</html>